$('.right-titleicon').on("click",function(e){
	projectVM.showMenulist()
})
let that;
projectVM = new Vue({
	el: "#content",
	data: {
		disabled:true,
		showSelect:false,
		menuList:[
			{name:'投入',path:'goIn.html'},{name:'退出',path:'goOut.html'},{name:'列表',path:'index.html'},
		],
		typeList:[{name:'变压器1'},{name:'变压器2'},{name:'变压器3'},{name:'变压器4'},],
		showSubmit:false,
		onStatus:'',
		selectText:'变压器1',
		result:'',
		showMenu:false,
		showMorebtn:false,
		isShowTerminalTransfer:false,
		isSingle:false,
		advance:'',
		showAdd:false,
		show_popup:false,
		OperateStaff:"",//操作人
		TutelageStaff:"",//监护人
		inName:"",//投入执行人
		onlyselect:false,
		mDisable:false,
		pageType:"21",
		editDATA:[],//获取的详情数据 存储原因是用于 获取主表数据 不做其他用处
		fileIds:"",
		itemData:{},//列表数据
		idea:"",//审批意见
		type:"",//审批意见
		listin:[
			{	
				isLumpFirst:false,
				isLumpFirstAdd:true,
				plateCode:'',
				plateName:'',
				deviceName:"",
				inCause:"",
				inTime:"",
				}
		],
		fileList:[],
		listout:[],
	},	
	components:{ 
		personselect,
		selectitem
	},
	created: function() {
		that = this;
	},
	mounted() {
		//hu获取页面必要的参数
		let config = mui.neioConfig.getNeioConfig();
		let key = config.KEYS.PLATENDATA;
		let data = mui.neioConfig.getStorage(key);
		let dataJson = JSON.parse(data)
		that.itemData = dataJson;
		that.pageType = dataJson.status;
		that.inName = dataJson.inOperateStaffName == null ?  "["+dataJson.outOperateStaffCode+"]"+dataJson.outOperateStaffName : "["+dataJson.inOperateStaffCode+"]"+dataJson.inOperateStaffName
		that.TutelageStaff = dataJson.inTutelageStaffName == null ?  "["+dataJson.outTutelageStaffCode+"]"+dataJson.outTutelageStaffName : "["+dataJson.inTutelageStaffCode+"]"+dataJson.inTutelageStaffName
		//设置 投入监护人
		
		that.$nextTick(this.$refs.personselect.person = that.TutelageStaff)
		that.$nextTick(this.$refs.personselect.personids = dataJson.inOperateStaffCode)
		//获取数据
		that.getoutDetail(dataJson)
		that.getInDetail(dataJson)
	},
	methods: {
		//获取投入详情
		getInDetail(data){
			if(data.inOperateId == null || data.inOperateId == "" || data.inOperateId == undefined){
				return;
			}
			let config = mui.neioConfig.getNeioConfig();
			let url = config.URL.queryPlateInfoById
			let params = {}
			params.inOperateId = data.inOperateId;
			params.plateCode = data.plateCode;
			mui.neiosRequst.httpRequst(url,params,function(ret){
			console.log("投入成功回掉："+JSON.stringify(ret))
				if(ret.code == 0){
					that.listin = ret.data;
					that.editDATA = that.editDATA.concat(ret.data);
					that.fileIds = ret.attachIds;
					
					that.listin.forEach((item,i)=>{
						if(that.listin.length == 1){
							item.isLumpFirst = false;
							item.isLumpFirstAdd = true
						}
						if(that.listin.length > 1){
							if(i == 0){
								item.isLumpFirst = false;
								item.isLumpFirstAdd = true
							}else{
								item.isLumpFirst = true;
								item.isLumpFirstAdd = true
							}
						}
					})
					that.getFileList(ret.attachIds);
					that.setCode()
				}
			},function(err){
				console.log("失败回掉："+JSON.stringify(err))
			})
		},
		//设置 设备编号
		setCode(){
			setTimeout(	function(){
				that.listin.forEach((its,i)=>{
					let a = that.$refs;
					let GV = 'selectitem'+i
					let chooseData = that.$refs[GV][0].chooseItem;
					that.$nextTick(that.$refs[GV][0].formInline.member = its.plateCode)
				})
			}, 100);

		},
		// 获取退出详情
		getoutDetail(data){
			if(data.outOperateId == null || data.outOperateId == "" || data.outOperateId == undefined){
				return;
			}
			let config = mui.neioConfig.getNeioConfig();
			let url = config.URL.queryPlateInfoById
			let params = {}
			params.outOperateId = data.outOperateId;
			params.plateCode = data.plateCode;
			mui.neiosRequst.httpRequst(url,params,function(ret){
				console.log("退出成功回掉："+JSON.stringify(ret))
				if(ret.code == 0){
					that.listout = ret.data;
					that.getFileList(ret.attachIds);
				}
			},function(err){
				console.log("失败回掉："+JSON.stringify(err))
			})
		},
		// 根据attachIds（网络获取）获取文件列表
		getFileList(attachIds){
			let config = mui.neioConfig.getNeioConfig();
			let url = config.URL.getFileList
			let params = {}
			params.fileIds = attachIds;
			mui.neiosRequst.httpRequst(url,params,function(ret){
				if(ret.code == 0){
					that.fileList = ret.data
					console.log("文件列表获取回掉："+JSON.stringify(that.fileList))
				}
			},function(err){
				console.log("失败回掉："+JSON.stringify(err))
			})
		},
		downloadFile(position){
			let url = "/app/common/downloadFile";
			let param = {};
			param.fileId = that.fileList[position].fileId;
			mui.neiosCommon.downLoadFile(param,function(ret){
				mui.plusReady(function () {
					plus.nativeUI.closeWaiting();
				})
				//调用存储
				var callbackID = plus.bridge.callbackId(function(ret){
					mui.toast(ret[0])
				}, function(err){
					mui.toast('文件下载失败')
				});
				let fileName = that.fileList[position].fileName
				plus.bridge.execSync( "FileHelper", "doFileSave", [callbackID, ret, fileName, "Argus3", "Argus4"] )
			},function(err){
				mui.plusReady(function () {
					plus.nativeUI.closeWaiting();
				})
				mui.toast('文件下载失败')
			});
			
		},
		chooesFile(){
				mui.neiosCommon.doUploads(function(ret){
					if("0" == ret.code){
						console.log("成功："+JSON.stringify(ret))
						mui.toast('文件'+ret.data.fileName+"上传成功");
						that.fileIds == "" ? that.fileIds += ret.data.fileId : that.fileIds += ","+ret.data.fileId;
						
						let fileArray = that.fileList;
						let data = {fileName:ret.data.fileName,fileId:ret.data.fileId,uploadTime:ret.data.uploadTime,uploadUserName:ret.data.uploadUserName,doOne:""};
						fileArray.push(data)
					}else{
						mui.toast('文件上传失败');
					}
				},function(err){
					mui.toast('文件上传失败');
				})
				
		},				
		// 获取时间
		dateFormate () {
			return mui.neiosCommon.transTime(new Date(),"min")
		},
		//获取毫秒
		getMiu(){
			let date = that.dateFormate();
			var arr = date.split(/[- : \/]/);
			var startDate = Date.parse(new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4]));
			return startDate;
		},
		//获取uuid
		getUUid(){
			let uuid = mui.neiosCommon.getUuidv4();
			return uuid;
		},
		//获取用户信息-新建投退记录的情况下
		getUserInfo(){
			let user = JSON.parse(localStorage.getItem("neiosUser"))
			return user;
		},
		// 保存
		saveData(){
			let config = mui.neioConfig.getNeioConfig();
			let url = config.URL.saveOrUpdatePlateOperate;
			that.url = url;
			that.createData(21);
		},
		// 提交
		subData(){
			let config = mui.neioConfig.getNeioConfig();
			let url = config.URL.submitPlateOperate;
			that.url = url;
			that.createData(32);
		},
		//组装数据
		createData(status){
			let message = status == "21" ? "是否保存" : "是否提交";
			mui.confirm(message,'提示',['取消','确认'],function (e) {
				if(1 == e.index){
					
					let manTab = paramJson.getEditObj(that.editDATA,1,1,status,"in",that)
					let dataObj = paramJson.getInfoList(that.listin,manTab,"in")
					that.doInteNet(dataObj);
				}
			},'div')
		},
		//网络请求
		doInteNet(params){
			let isNull = mui.neioConfig.checkPlatenList(params.infoListParam,"in")
			if(!isNull) return;
			let paramStr = JSON.stringify(params);
			mui.neiosCommon.doLocalSql("platenProtectTab","NULL",'0','0',paramStr,function(ret){
				console.log(JSON.stringify(ret))
				if(ret == "localStorage Success"){
					mui.toast("已离线保存！")
					return;
				}
				if(ret == "INTEL"){
					mui.neiosRequst.httpRequst(that.url,params,function(ret){
						console.log(JSON.stringify(ret))
						if(ret.code == 0){
							mui.toast(ret.msg)
							setTimeout(that.goBack,700);
						}
						if( ret.code == 1){
							mui.toast(ret.msg)
						}
					},function(err){
						console.log("失败回掉："+JSON.stringify(err))
					})
				}
			})
		
		},
		netSure(){
			const param = {};
			let url = "";
			if(that.idea == ""){
				mui.toast("请输入审批意见")
				return;
			}
			if(that.type == "1"){
				let manTab = paramJson.getEditObj(that.editDATA,1,1,'21',"in",that)
				let dataObj = paramJson.getInfoList(that.listin,manTab,"in")
				
				let config = mui.neioConfig.getNeioConfig();
				url = config.URL.sendBackPlateOperate;
				
				param.workId = that.itemData.inWorkId;
				param.workType = "0205";
				param.nodeDealStaffCode = that.getUserInfo().userId;
				param.nodeDealStaffName = that.getUserInfo().name;
				param.approveDes = that.idea;
				param.params = dataObj;
				
				console.log("审批数据："+JSON.stringify(param)+"接口地址："+url)
			
			}
			
			if(that.type == "0"){
				console.log("AAAAAA:"+JSON.stringify(that.itemData))
				let manTab = paramJson.getEditObj(that.editDATA,1,1,'90',"in",that)
				let dataObj = paramJson.getInfoList(that.listin,manTab,"in")
				
				let config = mui.neioConfig.getNeioConfig();
				url = config.URL.approvePlateOperate;
				
				param.workId = that.itemData.inWorkId;
				param.workType = "0205";
				param.nodeDealStaffCode = that.getUserInfo().userId;
				param.nodeDealSta62ffName = that.getUserInfo().name;
				param.approveDes = that.idea;
				param.params = dataObj;
				
				param.params.plateOperate.workId = param.workId;
				
				console.log("审批数据："+JSON.stringify(param)+"接口地址："+url)
			
			}
			
			mui.neiosRequst.httpRequst(url,param,function(ret){
				console.log(JSON.stringify(ret))
				if(ret.code == 0){
					mui.toast(ret.msg)
					setTimeout(that.goBack,700);
				}
				if( ret.code == 1){
					mui.toast(ret.msg)
				}
			},function(err){
				console.log("失败回掉："+JSON.stringify(err))
			})
		},
		hiddenNetSure(){
			that.isShowTerminalTransfer = false;
			that.idea = "";
		},
		//审批操作
		goback(type){
			that.type = type;
			that.isShowTerminalTransfer = true;
		},
		FileDelete(position){
			mui.confirm("确认删除?",'提示',['取消','确认'],function (e) {
				if(1 == e.index){
					mui.toast("删除文件"+that.fileList[position].fileName)	
					debugger
					//获取所有已记录id的默认长度 用于对比验证
					let idLength = that.fileIds.length;
					let idStr;
					let id = that.fileList[position].fileId+","
					idStr = that.fileIds.replace(id,"")
					//获取已记录id的最新长度
					let newidLength = idStr.length;
					//验证长度 如果长度没有变化 则认为没有进行 删除id 
					if(idLength == newidLength){
						let ids = that.fileList[position].fileId
						idStr = that.fileIds.replace(ids,"")
					}
					//删除末尾的 “,”
					let Tm = idStr.substring(idStr.length-1)
					if(Tm == ","){
						idStr = idStr.substring(0,idStr.length-1)
					}
					that.fileIds = idStr;
					console.log(that.fileIds)
					that.fileList.splice(position,1)
				}
			},'div')
		},
		//返回上一页
		goBack(){
			let pageId = plus.webview.getWebviewById("dailyRecord/platenProtect/index.html");
			mui.fire(pageId, 'reloadPage', {result:"result"});
			mui.back()
		},
		//添加新的检查单
		addLupm(){
			this.listin.forEach( (item,i) =>{
				if(i == 0){
					item.isLumpFirst = true;
					item.isLumpFirstAdd = false;
				}
			})
			let newLumpItem = {
				isLumpFirst:true,
				isLumpFirstAdd:true,
				plateCode:'',
				plateName:'',
				deviceName:"",
				inCause:"",
				inTime:"",
			}
			this.listin.push(newLumpItem)
		},
		//移除检查单
		removeLupm(position){
			
			//移除页面传过来的对应的 item 
			// if(that.exitDataJson != ""){
			// 	that.exitDataJson.netDeviceInfo.forEach((item,i)=>{
			// 		if(item.deviceInfoId == this.list[position].deviceInfoId){
			// 			that.exitDataJson.netDeviceInfo.splice(i,1);
			// 		}
			// 	})
			// }
			this.listin.splice(position,1)
			if(this.listin.length <= 1){
				this.listin[0].isLumpFirst = false;
				this.listin[0].isLumpFirstAdd = true;
			}else{
				let lumpItemPosition = this.listin.length;
				this.listin[lumpItemPosition - 1].isLumpFirstAdd = true;
			}
		},
		chooseDate(position){
			this.dtPickrer.show(function(e){
				console.log(JSON.stringify(e),'==')
				that.listin[position].inTime = e.value;
			},false)
		},
		navTo(e){
			mui.openWindow({
					url: e,
					id: e,
			});
			}
	},
	
	})
	mui.init()
	projectVM.dtPickrer = new mui.DtPicker({
		type : 'datetime'
	})